/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import static java.awt.Frame.NORMAL;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

/**
 * JFrame通用类
 *
 * @author SoChishun
 * @version 1.0 2016-5-18 SoChishun Added.
 */
public class JFrameUtil {

    // 已打开的窗体集合 2016-5-17
    public static HashMap<String, Object> OpenForms = new HashMap<String, Object>();

    /**
     * 打开窗体
     *
     * @param <T>
     * @param c
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @version 1.0 2016-5-17 SoChishun Added.
     */
    public static <T extends JFrame> void openJFrame(Class<T> c, String text) {
        T frm;
        String frame_name = c.getName();
        // 已打开
        if (OpenForms.containsKey(frame_name)) {
            frm = (T) OpenForms.get(frame_name);
            frm.setState(NORMAL); // 如果最小化则恢复到普通状态
            frm.setVisible(true);
            return;
        }
        // 未打开
        try {
            frm = c.newInstance();
            frm.setTitle(text);
            frm.setLocationRelativeTo(null); // 居中对齐
            frm.setDefaultCloseOperation(DISPOSE_ON_CLOSE); // 关闭当前窗体但不关闭主窗体
            // 自定义关闭窗体方法 2016-5-177
            frm.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    OpenForms.remove(frame_name); // 从已打开窗体集合中移除
                }
            });
            OpenForms.put(frame_name, frm);
            frm.setVisible(true);
        } catch (InstantiationException ex) {
            Logger.getLogger(c.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(c.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // 2016-5-24
    public static <T extends JFrame> void openJFrame(String classname, String text) {
        T frm;
        String frame_name = classname;
        // 已打开
        if (OpenForms.containsKey(frame_name)) {
            frm = (T) OpenForms.get(frame_name);
            frm.setState(NORMAL); // 如果最小化则恢复到普通状态
            frm.setVisible(true);
            return;
        }
        // 未打开
        try {
            frm = (T) Class.forName(classname).newInstance();
            frm.setTitle(text);
            frm.setLocationRelativeTo(null); // 居中对齐
            frm.setDefaultCloseOperation(DISPOSE_ON_CLOSE); // 关闭当前窗体但不关闭主窗体
            // 自定义关闭窗体方法 2016-5-177
            frm.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    OpenForms.remove(frame_name); // 从已打开窗体集合中移除
                }
            });
            OpenForms.put(frame_name, frm);
            frm.setVisible(true);
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
            Logger.getLogger(JFrameUtil.class.getName()).log(Level.SEVERE,ex.getMessage());
        }
    }
}
